

//自己封装一个parse将后端给的数据转换为前端对象
function parse(javaData){
    let temp = {}
    //let javaData = '{"uname":"宇宙","age":"18"}'
//先把json数据格式的 {}," 去掉
javaData = javaData.replace(/{|}|"/g,'') 
//console.log(javaData);   uname:宇宙,age:18

//变数组:split是根据指定字符串分割字符成数组 ==>  javaDataArr[0] = 'uname:宇宙', javaDataArr[1]= 'age:18'
 javaDataArr = javaData.split(',')
//console.log(javaData);   ['uname:宇宙', 'age:18']

//遍历数组
javaDataArr.forEach(item =>{
    // console.log(item);  uname:宇宙   age:18
    //根据冒号将item分隔为数组 ==>
    let  newArr = item.split(':')
    //console.log(newArr);
    let key = newArr[0]
    let value = newArr[1]
    temp[key] = value
})
return temp
}
console.log(parse('{"uname":"宇宙","age":"18"}'));
 

//把js对象转为JSON对象
function stringify(webObj){
    //let webObj ={uname: '宇宙', age: '18'}
    let arr = []
    // 1 遍历对象
    for (let key in webObj)
    {
        // 2 遍历外面-定义空数组arr
        // 3 遍历里面-将  键拼接:拼接值 push到数组中
        // let temp = `"键":"值"`
        let temp = `"${key}":"${webObj[key]}"`
        // 4 遍历结束-将数组转字符串即可
        arr.push(temp)
    }
     //console.log(`{${arr}}`);
    return `{${arr}}`
}
console.log(stringify({uname: '宇宙', age: '18'}));